#delimit;
clear;
clear matrix;

#delimit;
global MY_OUT_PATH  "[path where your master data set is stored]"; 
global MY_TEMP_PATH "[path where your temporary files are stored]";
global MY_FIGURE_PATH "[path where your figures and corresponding data sets are stored]";
set mem 1000m;

set more off;
cap log close;  
#delimit;
use "${MY_OUT_PATH}NEW_CNEF3_LSsample.dta", clear;

*********************************************;
*********************************************;
*********************************************;

* check labor force status if working==0;
* lfs is current, while earnings are previous year - Change lfs to previous year;
#delimit;
preserve;
keep persnr year lfp;
replace year=year+1;
rename lfp lfp_new;
save "${MY_TEMP_PATH}changed_lfp", replace;
restore;

merge 1:1 persnr year using "${MY_TEMP_PATH}changed_lfp";
drop _merge;
drop lfp;

tab lfp_new if working==0;

**********************************************;
* sample selection;
*********************************************;

*keep households in the sample where at least one member is either working or unemployed (at least one member in labor force)
#delimit;
egen lf=rowtotal(lfp_new working);
bys yearhhnr: egen lfp_hh=total(lf);
keep if lfp_hh>0 & lfp_hh!=.;

* keep only the observation of head (i.e. 1 observation per household);
#delimit;
keep if head==1;

sum HHearningOECD if working==0, d;
sum HHearningOECD if working==1, d;


********************************************************;
* Abbildung: 
********************************************************;

#delimit;
preserve;
collapse (p10) p10HHearningOECD=HHearningOECD (p50) p50HHearningOECD=HHearningOECD (p90) p90HHearningOECD=HHearningOECD [pweight=w11102], by(year);
gen p9050HHearningOECD=p90HHearningOECD/p50HHearningOECD;
gen p1050HHearningOECD=p10HHearningOECD/p50HHearningOECD;
sort year;
list;
label var p1050HHearningOECD "mind. ein HH-Mitglied in LF";
label var p9050HHearningOECD "mind. ein HH-Mitglied in LF";
save "${MY_FIGURE_PATH}HHearningOECDpercentileratios.dta", replace;
restore;

